package com.ruoyi.workbench.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileTypeUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.workbench.domain.WorkFile;
import com.ruoyi.workbench.domain.WorkSystemManage;
import com.ruoyi.workbench.mapper.WorkFileMapper;
import com.ruoyi.workbench.mapper.WorkSystemManageMapper;
import com.ruoyi.workbench.service.IWorkSystemManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 制度管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-02
 */
@Service
public class WorkSystemManageServiceImpl implements IWorkSystemManageService {
    @Autowired
    private WorkSystemManageMapper workSystemManageMapper;

    @Autowired
    private WorkFileMapper workFileMapper;

    /**
     * 查询制度管理
     *
     * @param sid 制度管理主键
     * @return 制度管理
     */
    @Override
    public WorkSystemManage selectWorkSystemManageBySid(Integer sid) {
        WorkSystemManage systemManage = workSystemManageMapper.selectWorkSystemManageBySid(sid);
        systemManage.setWorkFiles(workFileMapper.selectAllByJoinSystemId(systemManage.getSid()));
        return systemManage;
    }

    /**
     * 查询制度管理列表
     *
     * @param workSystemManage 制度管理
     * @return 制度管理
     */
    @Override
    public List<WorkSystemManage> selectWorkSystemManageList(WorkSystemManage workSystemManage) {
        List<WorkSystemManage> workSystemManages = new ArrayList<>();
        List<Integer> ids = workSystemManage.getIds();
        if (ids!=null&&ids.size()>0){
            workSystemManages = workSystemManageMapper.selectAllByIdsIn(ids);
        }else {
            workSystemManages = workSystemManageMapper.selectWorkSystemManageList(workSystemManage);
        }

        for (WorkSystemManage systemManage : workSystemManages) {
            systemManage.setWorkFiles(workFileMapper.selectAllByJoinSystemId(systemManage.getSid()));
        }
        return workSystemManages;
    }

    /**
     * 新增制度管理
     *
     * @param workSystemManage 制度管理
     * @return 结果
     */
    @Override
    public int insertWorkSystemManage(WorkSystemManage workSystemManage) {
        SysUser loginUser = getLoginUser();
        workSystemManage.setUploadUaerId(loginUser.getUserId());
        workSystemManage.setUploadUserName(loginUser.getNickName());
        workSystemManage.setUplodaDate(new Date());
        workSystemManage.setCreateTime(DateUtils.getNowDate());
        int i = workSystemManageMapper.insertWorkSystemManage(workSystemManage);
        // 添加附件表信息
        List<JSONObject> fjbs = workSystemManage.getFileList();
        for (JSONObject json : fjbs) {
            JSONObject fjInfo = json.getJSONObject("response");
            if (StringUtils.isNotNull(fjInfo)) {
                WorkFile workFile = new WorkFile();
                workFile.setFileUrl(fjInfo.getString("url"));
                workFile.setFilePath(fjInfo.getString("filePath"));
                String fileName = fjInfo.getString("originalFilename");
                workFile.setFileName(fileName);
                workFile.setFileType(FileTypeUtils.getFileType(fileName));
                workFile.setCreateTime(DateUtils.getNowDate());
                workFile.setJoinSystemId(workSystemManage.getSid());
                workFileMapper.insertWorkFile(workFile);
            }
        }
        return i;
    }

    /**
     * 修改制度管理
     *
     * @param workSystemManage 制度管理
     * @return 结果
     */
    @Override
    public int updateWorkSystemManage(WorkSystemManage workSystemManage) {
        Integer sid = workSystemManage.getSid();
        workSystemManage.setUpdateTime(DateUtils.getNowDate());
        List<JSONObject> fjbs = workSystemManage.getFileList();
        workFileMapper.deleteByJoinSystemId(sid);
        for (JSONObject json : fjbs) {
            JSONObject fjInfo = json.getJSONObject("response");
            if (StringUtils.isNotNull(fjInfo)) {
                WorkFile workFile = new WorkFile();
                workFile.setFileUrl(fjInfo.getString("url"));
                workFile.setFilePath(fjInfo.getString("filePath"));
                String fileName = fjInfo.getString("originalFilename");
                workFile.setFileName(fileName);
                workFile.setFileType(FileTypeUtils.getFileType(fileName));
                workFile.setCreateTime(DateUtils.getNowDate());
                workFile.setJoinSystemId(sid);
                workFileMapper.insertWorkFile(workFile);
            } else {
                WorkFile workFile = new WorkFile();
                workFile.setFileUrl(json.getString("url"));
                workFile.setFilePath(json.getString("filePath"));
                String fileName = json.getString("name");
                workFile.setFileName(fileName);
                workFile.setFileType(FileTypeUtils.getFileType(fileName));
                workFile.setCreateTime(DateUtils.getNowDate());
                workFile.setJoinSystemId(sid);
                workFileMapper.insertWorkFile(workFile);
            }
        }
        return workSystemManageMapper.updateWorkSystemManage(workSystemManage);
    }

    /**
     * 批量删除制度管理
     *
     * @param sids 需要删除的制度管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkSystemManageBySids(Integer[] sids) {
        List<Integer> idList = Arrays.asList(sids);
        workFileMapper.deleteByJoinSystemIdIn(idList);// 删除关联附件
        return workSystemManageMapper.deleteWorkSystemManageBySids(sids);
    }

    /**
     * 删除制度管理信息
     *
     * @param sid 制度管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkSystemManageBySid(Integer sid) {
        return workSystemManageMapper.deleteWorkSystemManageBySid(sid);
    }

    private SysUser getLoginUser() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return loginUser.getUser();
    }

    private String getSid() {
        return IdUtils.fastSimpleUUID();
    }

}
